package org.ftz.JudgeOnline.Java;
import org.ftz.JudgeOnline.Java.Answer;
import org.ftz.JudgeOnline.Java.ExecUtil;
import java.io.*;
public class Test {
    public static void main(String[] args) throws IOException { //测试代码
        String DIR="D:\\BigProgram\\";
        String DIR1="D:\\BigProgram";
        String javaFile=DIR+"Main.java";
        String javaClass="Main";
        String compileCmd=String.format("javac -encoding utf8 %s -d %s",javaFile,DIR1);
        String runningCmd=String.format("cmd /c java -classpath %s %s < D:\\BigProgram\\1.txt", DIR1, javaClass);
        System.out.println(compileCmd+"\n"+runningCmd);
        String doc= "public class Main {\n" +
                "    public static void main(String[] args) {\n" +
                "        Syste.out.println(15);\n" +
                "    }\n" +
                "}";
        FileWriter writer=new FileWriter(javaFile); //将代码写入到定义路径下特定的java源文件中
        writer.write(doc);
        writer.close();
        Answer answer= ExecUtil.run(compileCmd,false,true); //编译源文件为class文件
        if(answer.getError()==0) { //若编译成功即可开始运行
            answer = ExecUtil.run(runningCmd, true, true);
            if(answer.getError()!=0)
                answer.setReason(Answer.Success);
            else
                answer.setReason(Answer.RuntimeError);
            System.out.println(answer.getStdout());
        }else{
            answer.setReason(Answer.Error);
            System.out.println(answer.getStderr());
        }
    }
}

